<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="qq" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
      <el-dialog
        :title="isEdit ? '添加':'编辑'"
        :visible.sync="showDialog"
        width="50%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <add-or-edit v-if="showDialog" :id="curId" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartmentList, deleteDepartmentById } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false // 是否处于编辑状态
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    async loadDepartmentList() {
      try {
        const res = await getDepartmentList()
        // 删除传智教育公司
        res.data.depts.shift()
        // console.log(res.data.depts)
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log(e)
      }
    },
    hShowAdd(id) {
      // 弹出框显示
      this.showDialog = true
      // 吧id传给子组件，在子组件中获取详情
      this.curId = id
      // 传递表示，告诉是添加状态
      this.isEdit = false
    },
    hShowEdit(id) {
      this.showDialog = true
      // 吧id传给子组件，在子组件中获取详情
      this.curId = id
      // 传递表示，告诉是添加状态
      this.isEdit = true
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartmentList()
    },
    hDel(id) {
      // 提示用户真的要删除吗
      this.$confirm('真的要删除吗?嘤嘤嘤!', '提示', {
        type: 'warning'
      })
        .then(async() => {
          try {
            // 发请求
            const res = await deleteDepartmentById(id)
            // 根据结果提醒用户
            this.$message.success(res.message)
            // 重新渲染页面
            this.loadDepartmentList()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    }
  }

}

</script>
